const initConfig = require('../configs/bulletConfig');
//
module.exports = class {
    constructor() {
        this.bulletMap = {};
        this.bulletPrefab = null;
        this.bulletPool = new cc.NodePool();
        this.curLevel = 0;
        this.fireId = null;
        //
        this.init();
    }

    init() {
        console.log('子弹配置', initConfig);
        //子弹节点层
        this.bulletLayer = cc.find('Canvas/layerGame/gameLayerButtle');
    }

    loadBulletRes() {
        ICPC.addOneLoad();
        cc.loader.loadRes(initConfig.bulletPrefabPath, cc.Prefab, (error, resource) => {
            if (error) {
                throw error;
            }
            if (!(resource instanceof cc.Prefab)) {
                console.error('Prefab error', initConfig.bulletPrefabPath);
                return;
            }
            this.bulletPrefab = resource;
            ICPC.emit('loadBulletResEnd');
            ICPC.oneLoadOver();
        });
    }

    //私用
    newBullet() {
        if (this.bulletPool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            return this.bulletPool.get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
            return cc.instantiate(this.bulletPrefab);
        }
    }

    //私用
    deleteBullet(bulletNode) {
        this.bulletPool.put(bulletNode);
    }

    getBullet(id) {
        return this.bulletMap[id];
    }

    onFire() {
        const conf = this.getBulletLevelInfo();
        for (let i in conf.xArray) {
            let x = conf.xArray[i];
            let xPos = conf.xPosArray[i];
            this.addBullet({used: false, velocity: conf.velocity, icon: conf.icon, x, xPos});
        }
        ICPC.soundManager.playBullet();
    }

    startFire() {
        if (ICPC.gameDataManager.egg >= 25) {
            this.curLevel = initConfig.bulletLevels.length - 1;
        }
        ICPC.gameDataManager.egg = null;
        //
        if (this.fireId) {
            this.stopFire();
        }
        this.fireId = setInterval(this.onFire.bind(this), this.getBulletLevelInfo().speed);
    }

    stopFire() {
        if (this.fireId) {
            clearInterval(this.fireId);
        }
    }

    onScoreChange(score) {
        const bulletLevelInfo = this.getBulletLevelInfo();
        if (score >= bulletLevelInfo.upScore) {
            if (this.levelUpBullet()) {
                ICPC.effectManager.addEffect({effectName: 'message1', effectInfo: '武器升级'}, cc.v2(0, 0));
            } else {
                //武器已经满级
            }
        }
    }

    levelUpBullet() {
        let levelInfo = initConfig.bulletLevels;
        this.curLevel++;
        if (this.curLevel >= levelInfo.length) {
            //子弹满级
            this.curLevel--;
            return false;
        }
        this.startFire();
        return true;
    }

    getBulletLevelInfo() {
        return initConfig.bulletLevels[this.curLevel];
    }

    addBullet(bulletInfo) {
        //
        const id = ICPC.randId();
        let bulletNode = this.newBullet();
        this.bulletMap[id] = {
            m_node: bulletNode,
            m_info: bulletInfo,
        };
        this.bulletLayer.addChild(bulletNode);
        const fly = ICPC.flyManager.getFlyNode();
        let {x, y} = fly.getPosition();
        y += fly.getChildByName('icon').height + ICPC.flyManager.flyLayer.y;
        bulletNode.getComponent('bullet').initBullet(id, bulletInfo.icon, bulletInfo.x, bulletInfo.velocity, cc.v2(x + bulletInfo.xPos, y));
        //通知外部
        ICPC.emit('addBullet', id);
    }

    isStaticBullet(id) {
        let bullet = this.bulletMap[id];
        if (!bullet) {
            // console.warn('#77 无效子弹 可能在同一帧同时碰撞多个敌机');
            return true;
        }
        //
        return bullet.m_info.used;
    }

    setStaticBullet(id) {
        let bullet = this.bulletMap[id];
        if (!bullet) {
            console.error('#66 无效子弹');
            return;
        }
        //
        bullet.m_node.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, 0);
        bullet.m_info.used = true;
        //
    }

    killBullet(id) {
        let bullet = this.bulletMap[id];
        if (bullet) {
            this.deleteBullet(bullet.m_node);
            delete this.bulletMap[id];
        } else {
            console.error('#129 无效子弹');
        }
    }

    clearAll() {
        this.curLevel = 0;
        this.fireId = null;
        for (let id in this.bulletMap) {
            this.killBullet(id);
        }
    }
}